#include <qpe/config.h>
#include <qwidget.h>
#include <qlineedit.h>
#include <qcheckbox.h>
#include <qspinbox.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include <qvgroupbox.h>
#include <qhgroupbox.h>
#include <qlayout.h>
#include <qevent.h>

#include "ConfigWidget.h"

ConfigWidget::ConfigWidget(Config *conf, QWidget *parent): QWidget(parent),
		_config(conf)
{
	QVGroupBox *conGroup = new QVGroupBox(tr("Connection"), this);
	_config->setGroup("Host");
	new QLabel(tr("Host IP Address: "), conGroup);
	_hostIPAddress = new QLineEdit(_config->readEntry("HostIPAddress", "192.168.0.2"), conGroup);
	// listview, combobox...
	_port = new QSpinBox(1, 65535, 1, conGroup);
	_port->setValue(_config->readNumEntry("Port", 7378));
	_port->setPrefix(tr("Port: "));
	
	QVGroupBox *mouseGroup = new QVGroupBox(tr("Mouse"), this);
	_config->setGroup("Mouse");
	_mouseMoveAbsolute = new QCheckBox(tr("mouse move absolute"), mouseGroup);
	_mouseMoveAbsolute->setChecked(_config->readBoolEntry("MouseMoveAbsolute", false));
	_enableTapClick = new QCheckBox(tr("enable tap click"), mouseGroup);
	_enableTapClick->setChecked(_config->readBoolEntry("EnableTapClick", false));
	_cursorAccel = new QSpinBox(10, 1000, 10, mouseGroup);
	_cursorAccel->setValue(_config->readNumEntry("CursorAccel", 100));
	_cursorAccel->setPrefix(tr("Cursor Accel: "));
	_cursorAccel->setSuffix(tr("%"));
	_tapClickMargin = new QSpinBox(0, 16, 1, mouseGroup);
	_tapClickMargin->setValue(_config->readNumEntry("TapClickMargin", 2));
	_tapClickMargin->setPrefix(tr("Tap Click Margin: "));
	_tapClickMargin->setSuffix(tr("px"));
	
	QVGroupBox *optGroup = new QVGroupBox(tr("Option"), this);
	_config->setGroup("Option");
	_disableKeyHelper = new QCheckBox(tr("disable KeyHelper"), optGroup);
	_disableKeyHelper->setChecked(_config->readBoolEntry("DisableKeyHelper", true));
	
	QHGroupBox *opeGroup = new QHGroupBox(tr("Operation"), this);
	QPushButton *b;
	b = new QPushButton(tr("Quit"), opeGroup);
	connect(b, SIGNAL(clicked()), this, SIGNAL(quitButtonClicked()));
	b = new QPushButton(tr("LCD Off"), opeGroup);
	connect(b, SIGNAL(clicked()), this, SIGNAL(turnOffDisplayButtonClicked()));
/*
	b = new QPushButton(tr("Rotate"), opeGroup);
	connect(b, SIGNAL(clicked()), this, SIGNAL(rotateScreenButtonClicked()));
*/
	b = new QPushButton(tr("KeyHelper On"), opeGroup);
	connect(b, SIGNAL(clicked()), this, SIGNAL(enableKeyHelperButtonClicked()));
	b = new QPushButton(tr("KeyHelper Off"), opeGroup);
	connect(b, SIGNAL(clicked()), this, SIGNAL(disableKeyHelperButtonClicked()));
	
	QVBoxLayout *l = new QVBoxLayout(this);
	l->addWidget(opeGroup);
	l->addWidget(conGroup);
	l->addWidget(mouseGroup);
	l->addWidget(optGroup);
}

ConfigWidget::~ConfigWidget()
{
}

void ConfigWidget::save()
{
	_config->setGroup("Host");
	_config->writeEntry("HostIPAddress", hostIPAddress());
	_config->writeEntry("Port", port());
	
	_config->setGroup("Mouse");
	_config->writeEntry("MouseMoveAbsolute", mouseMoveAbsolute());
	_config->writeEntry("EnableTapClick", isEnableTapClickChecked());
	_config->writeEntry("CursorAccel", cursorAccel());
	_config->writeEntry("TapClickMargin", tapClickMargin());
	
	_config->setGroup("Option");
	_config->writeEntry("DisableKeyHelper", isDisableKeyHelperChecked());
}

QString ConfigWidget::hostIPAddress() const
{
	return _hostIPAddress->text();
}

int ConfigWidget::port() const
{
	return _port->value();
}

bool ConfigWidget::mouseMoveAbsolute() const
{
	return _mouseMoveAbsolute->isChecked();
}

bool ConfigWidget::isEnableTapClickChecked() const
{
	return _enableTapClick->isChecked();
}

int ConfigWidget::cursorAccel() const
{
	return _cursorAccel->value();
}

bool ConfigWidget::isDisableKeyHelperChecked() const
{
	return _disableKeyHelper->isChecked();
}

int ConfigWidget::tapClickMargin() const
{
	return _tapClickMargin->value();
}
